// If you have a custom title screen, this function is all that needs to be changed
// This function is called after the splash is over to decide what to do next, by default it'll proceed with 

function Splash_NextPhase
	ResetObjectEntity(object.entityPos, TypeName[Sonic Team], 0, object.xpos, object.ypos)
end function

// Everything hereon is the splash code, you shouldn't need to change this

// Array of every frame and what sprite to use
private table Splash_SpriteTable
	2, 1, 0, 1, 2, 1, 0, 2, 0, 1, 2, 0, 2, 1, 0, 1, 0, 1, 2, 1, 0, 2, 0, 2, 1, 0, 2, 0, 1, 2, 0, 2, 0, 2, 1, 0, 1, 0, 2, 0, 1, 2, 0, 2, 1, 0, 1, 2, 1, 2, 1, 0, 2, 0, 1, 2
end table

// Array of how long every frame lasts for
private table Splash_frameDelay
	64, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 4, 3, 1, 2, 3, 2, 4, 3, 3, 4, 30, 4, 2, 3, 2, 3, 2, 2, 3, 2, 3, 2, 2, 3, 2, 64, 2, 3, 2, 2, 3, 2, 2, 3, 2, 2, 2, 2, 2, 3, 2, 3, 2, 360
end table

event ObjectMain
	if inputPress.start == 1
		if object.state < 5
			object.state = 5
			//object.value0 = 0
		end if
	end if
	
	switch object.state
	case 0
		// Initialization phase
		object.value0 = 320
		SetClassicFade(0, 0, 0, object.value0)
		object.state++
		break
	case 1
		// Fade-in phase
		if object.value0 > 0
			object.value0 -= 8
		else
			object.state++
		end if
		
		SetClassicFade(0, 0, 0, object.value0)
		break
	case 2
		// Main phase

		// Move to next frame in accordance with the table
		GetTableValue(temp0, object.frame, Splash_frameDelay)
		object.value1++
		if object.value1 == temp0
			object.frame += 1
			object.value1 = 0
		end if

		// Play the jingle when the logo starts to flash
		if object.frame == 1
			if object.value0 == 0
				PlayMusic(10)
			end if
		end if

		object.value2++
		if object.value2 == 150
			StopMusic()
			PlaySfx(SfxName[PowerOut], 0)
			object.state++
			object.value2 = 0
			object.alpha = 0
		end if

		break
	case 3
		object.value2++
		if object.value2 == 100
			PlaySfx(SfxName[Freddy1], 0)	
		end if

		if object.value2 >= 200
			if object.alpha < 255
				object.alpha += 8
			end if
		end if

		if object.value2 == 500
			object.state++
			object.value2 = 0
		end if
	break
	case 4
		if object.alpha > 0
			object.alpha-=8
		end if

		if object.alpha == 0
			object.state = 5
		end if
	break
	case 5
		// Fade-out phase
		if object.value0 < 256
			object.value0 += 8
			SetClassicFade(0, 0, 0, object.value0)
		else
			Callfunction(Splash_NextPhase)
			SetClassicFade(0, 0, 0, 255)
		end if
		break
	end switch
end event

event ObjectDraw
	if object.state < 3
		GetTableValue(temp0, object.frame, Splash_SpriteTable)
		DrawSpriteScreenXY(temp0, screen.xcenter, screen.ycenter)
	end if
	switch object.state
	case 0
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 255)
	break
	case 3
	case 4
		object.inkeffect = INK_ALPHA
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 255)
		DrawSpriteScreenFX(3, FX_INK, screen.xcenter, screen.ycenter)
	break
	case 5
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 255)
	break
	end switch
end event

event ObjectStartup
	LoadSpriteSheet("Title/Splash.gif")
	SpriteFrame(-160, -112, 320, 224, 0, 0)
	SpriteFrame(-160, -112, 320, 224, 0, 224)
	SpriteFrame(-160, -112, 320, 224, 0, 448)

	SpriteFrame(-60, -30, 120, 58, 321, 1)

	LoadPalette("Splash.act", 0, 50, 50, 63)
	SetMusicTrack("Splash.ogg", 10, 0)
end event


event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
